"""Aliases for mojo packages."""

_PACKAGES = {
    "stdlib": "mojo/stdlib/stdlib",
    "python": "mojo/python/mojo",
    "test_utils": "mojo/stdlib/test/test_utils",
}

def _mojo_aliases_impl(rctx):
    alias_rules = []
    for name, target in _PACKAGES.items():
        alias_rules.append("""
alias(
    name = "{name}",
    actual = "@//{prefix}{target}",
)""".format(name = name, target = target, prefix = "{prefix}"))

    build_content = """package(default_visibility = ["//visibility:public"])
{aliases}

""".format(aliases = "".join(alias_rules))

    rctx.file("BUILD.bazel", content = build_content.format(prefix = rctx.attr.prefix))
    rctx.file("mojo.bzl", content = """
ALL_MOJOPKGS = [
{packages}
]
""".format(packages = ",\n".join(['    "@mojo//:{}"'.format(name) for name in _PACKAGES.keys() if name != "python"])))

mojo_aliases = repository_rule(
    implementation = _mojo_aliases_impl,
    attrs = {
        "prefix": attr.string(
            doc = "The prefix of the modular/modular repo root",
            default = "",
        ),
    },
)
